package com.miao.teacher.ui;

import java.util.ArrayList;
import java.util.List;

import com.miao.teacher.presenters.OnReviseListener;
import com.miao.teacher.utils.Utils;
import com.miao.teacher.widget.SeekBarPlayer;
import com.miao.teacher.widget.WaitingDialog;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class RateFragment extends BaseFragment {

	private OnReviseListener mListener = null;

	private List<LinearLayout> mDeliveryList = new ArrayList<LinearLayout>();
	private TextView tvDeliveryScore;

	private TextView tvLanguageScore;
	private List<LinearLayout> mLanguageList = new ArrayList<LinearLayout>();

	private TextView tvDevScore;
	private List<LinearLayout> mDevList = new ArrayList<LinearLayout>();

	// private int mGray = Color.argb(155, 190, 190, 190);
	// private int mGreen = Color.argb(155, 138, 196, 76);

	private SeekBarPlayer mSeekBarPlayer = null;

	private SeekBar sBar = null;

	private TextView curTimeTv;
	private TextView totalTimeTv;
	private Button btnPlay = null;
	private Button btnPre = null;
	private Button btnNext = null;
	
	private boolean mIsPlay = false;

	private WaitingDialog waitDialog;

	/*
	 * 创建一个实例
	 */
	public static RateFragment newInstance() {
		RateFragment fragment = new RateFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.layout_my_rate, null);
		initView(view);
		showView();
		return view;
	}

	private void initView(View view) {
		waitDialog = new WaitingDialog(getActivity(), "载入中...");
		mDeliveryList.clear();
		mLanguageList.clear();
		mDevList.clear();
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star1));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star2));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star3));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star4));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star5));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star6));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star7));
		mDeliveryList.add((LinearLayout) view
				.findViewById(R.id.lly_delivery_star8));

		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star1));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star2));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star3));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star4));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star5));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star6));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star7));
		mLanguageList.add((LinearLayout) view
				.findViewById(R.id.lly_lanuage_star8));

		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star1));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star2));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star3));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star4));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star5));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star6));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star7));
		mDevList.add((LinearLayout) view.findViewById(R.id.lly_tDev_star8));

		for (int i = 0; i < mDeliveryList.size(); i++) {
			mDeliveryList.get(i).setTag(i + 1);
			mDeliveryList.get(i).setOnClickListener(new DeliveryOnClick());
		}
		for (int i = 0; i < mLanguageList.size(); i++) {
			mLanguageList.get(i).setTag(i + 1);
			mLanguageList.get(i).setOnClickListener(new LanguageOnClick());
		}
		for (int i = 0; i < mDevList.size(); i++) {
			mDevList.get(i).setTag(i + 1);
			mDevList.get(i).setOnClickListener(new DevStarOnClick());
		}
		tvDeliveryScore = (TextView) view.findViewById(R.id.tv_delivery_score);
		tvLanguageScore = (TextView) view.findViewById(R.id.tv_lanuage_score);
		tvDevScore = (TextView) view.findViewById(R.id.tv_tDev_score);

		curTimeTv = (TextView) view.findViewById(R.id.tvCurTime);
		totalTimeTv = (TextView) view.findViewById(R.id.tvTotalTime);
		sBar = (SeekBar) view.findViewById(R.id.sBar);
/*		sBar.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				switch (arg1.getAction()) {
				case MotionEvent.ACTION_DOWN:
					break;
				case MotionEvent.ACTION_UP:
					if (mSeekBarPlayer == null)
						return false;
					
					 * if (sBar.getProgress() != mSeekBarPlayer
					 * .getCurrentPosition()) { return false; }
					 
					if (mSeekBarPlayer.isReady()) {
						if (mSeekBarPlayer.isPlaying()) {
							mSeekBarPlayer.pause();
						} else {
							mSeekBarPlayer.play();
						}
					}
					if (mIsPlay) {
						mIsPlay = false;
						sBar.setThumb(getActivity().getResources().getDrawable(
								R.drawable.seek_thumb_play));
					} else {
						mIsPlay = true;
						sBar.setThumb(getActivity().getResources().getDrawable(
								R.drawable.seek_pause));

					}
					break;
				default:
					break;
				}
				return false;
			}
		});*/
		if (mSeekBarPlayer == null) {
			sBar.postDelayed(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					mSeekBarPlayer = SeekBarPlayer.create(mListener
							.getReviseJson().audio_url, sBar, curTimeTv,
							totalTimeTv);
					if (mSeekBarPlayer != null) {
						mSeekBarPlayer
								.setOnCompletionListener(new OnCompletionListener() {
									@Override
									public void onCompletion(MediaPlayer arg0) {
										mIsPlay = false;
										sBar
												.setThumb(getActivity()
														.getResources()
														.getDrawable(
																R.drawable.seek_thumb_play));
										mSeekBarPlayer.seekTo(0);
										btnPlay
										.setBackgroundResource(R.drawable.iv_audio_play);
									}
								});
					}
					Utils.showProgress(waitDialog, false);
				}
			}, 100);
			Utils.showProgress(waitDialog, true);
		}else
		{
			mSeekBarPlayer.setView(sBar, curTimeTv,
							totalTimeTv);
		}
		btnPlay = (Button) view.findViewById(R.id.btnPlayerPlay);
		btnPre = (Button) view.findViewById(R.id.btnPlayerPre);
		btnNext = (Button) view.findViewById(R.id.btnPlayerNext);
		btnPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer != null) {
					if (mSeekBarPlayer.isReady()) {
						if (mSeekBarPlayer.isPlaying()) {
							mSeekBarPlayer.pause();
							arg0
									.setBackgroundResource(R.drawable.iv_audio_play);
						} else {
							mListener.doStop(0);
							mSeekBarPlayer.play();
							arg0
									.setBackgroundResource(R.drawable.iv_audio_pause);
						}
					}

				} else {
					arg0.setBackgroundResource(R.drawable.iv_audio_pause);
				}
			}
		});
		btnPre.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.backward();
				}
			}
		});
		btnNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mSeekBarPlayer == null)
					return;
				if (mSeekBarPlayer.isReady()) {
					mSeekBarPlayer.forward();
				}
			}
		});
	}

	private void showView() {
		tvDeliveryScore.setText(mListener.getReviseJson().delivery_score + "'");
		tvLanguageScore.setText(mListener.getReviseJson().language_score + "'");
		tvDevScore.setText(mListener.getReviseJson().topic_score + "'");
		for (int i = 0; i < mDeliveryList.size(); i++) {
			if ((i + 1) * 0.5 > mListener.getReviseJson().delivery_score) {
				mDeliveryList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_normal));
			} else {
				mDeliveryList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_select));
			}
		}
		for (int i = 0; i < mLanguageList.size(); i++) {
			if ((i + 1) * 0.5 > mListener.getReviseJson().language_score) {
				mLanguageList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_normal));
			} else {
				mLanguageList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_select));
			}
		}
		for (int i = 0; i < mDevList.size(); i++) {
			if ((i + 1) * 0.5 > mListener.getReviseJson().topic_score) {
				mDevList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_normal));
			} else {
				mDevList.get(i).setBackgroundDrawable(
						getActivity().getResources().getDrawable(
								R.drawable.iv_rate_select));
			}
		}
	}

	class DeliveryOnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int index = Integer.parseInt(v.getTag().toString());
			mListener.getReviseJson().delivery_score = (float) (index * 0.5);
			mListener.doSave();
			showView();
		}

	}

	class LanguageOnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int index = Integer.parseInt(v.getTag().toString());
			mListener.getReviseJson().language_score = (float) (index * 0.5);
			mListener.doSave();
			showView();
		}

	}

	class DevStarOnClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int index = Integer.parseInt(v.getTag().toString());
			mListener.getReviseJson().topic_score = (float) (index * 0.5);
			mListener.doSave();
			showView();
		}

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		pauseAudio();
	}

	public void pauseAudio() {
		try {
			if (mSeekBarPlayer != null) {
				if (mSeekBarPlayer.isPlaying()) {
					mSeekBarPlayer.pause();
				}
			}
		} catch (Exception er) {
		}
	}
	public void pauseAudio2() {
		try {
			if (mSeekBarPlayer != null) {
				if (mSeekBarPlayer.isPlaying()) {
					btnPlay.performClick();
				}
			}
		} catch (Exception er) {
		}
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try {
			mListener = (OnReviseListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ "must implement OnReviseListener");
		}
	}

}
